package com.ai.service;

import com.ai.bo.CandidateBO;
import com.ai.bo.InterviewRecordBO;
import com.ai.mapper.CandidateMapper;
import com.ai.service.impl.CandidateServiceImpl;
import com.ai.vo.CandidateVO;
import com.ai.vo.InterviewRecordVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.micrometer.common.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import wst.ai.pojo.candidate.Candidate;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CandidateService implements CandidateServiceImpl {

    @Autowired
    private CandidateMapper candidateMapper;

    /**
     * 根据手机号查询候选人是否存在
     * @param mobile
     * @return
     */
    @Override
    public Candidate candidateExit(String mobile) {
        return candidateMapper.selectOne(new QueryWrapper<Candidate>().eq("mobile",mobile));
    }

    @Override
    public Candidate getCandidateById(String candidateId) {
        return candidateMapper.selectById(candidateId);
    }

    /**
     * 新增候选人
     * @param candidateBO
     */
    @Override
    public void createOrUpdate(CandidateBO candidateBO) {
        Candidate candidate = new Candidate();
        BeanUtils.copyProperties(candidateBO,candidate);
        candidate.setUpdatedTime(LocalDateTime.now());
        if (StringUtils.isBlank(candidate.getId())){
            candidate.setCreateTime(LocalDateTime.now());
            candidateMapper.insert(candidate);
        }else {
            candidateMapper.updateById(candidate);
        }
    }

    /**
     * 获取候选人列表
     * @param interviewRecordBO
     * @return
     */
    @Override
    public PageInfo<CandidateVO> candidateList(InterviewRecordBO interviewRecordBO) {
        PageHelper.startPage(interviewRecordBO.getPage(), interviewRecordBO.getPageSize());
        List<CandidateVO> candidateVOS = candidateMapper.candidateList(interviewRecordBO);
        PageInfo<CandidateVO> pageInfo = new PageInfo<>(candidateVOS);
        return pageInfo;
    }

    /**
     * 删除
     * @param candidateId
     */
    @Override
    public void deleteCandidate(String candidateId) {
        candidateMapper.deleteById(candidateId);
    }

}
